/*
 * RMBackwardValidationAgent.java    ver   06/dic/2009
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions.management.rm;

import org.mentalsmash.tazio.commons.identifiers.OID;
import org.mentalsmash.tazio.transactions.TransactionalSystemRuntimeException;
import org.mentalsmash.tazio.transactions.log.TransactionsLog;
import org.mentalsmash.tazio.transactions.management.BackwardValidationAgent;
import org.mentalsmash.tazio.transactions.management.TazioTransaction;
import org.mentalsmash.tazio.transactions.management.TransactionalOperation;
import org.mentalsmash.tazio.transactions.objects.RootSet;
import org.mentalsmash.tazio.transactions.objects.TransactionalObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

/**
 * @version ver 06/dic/2009
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
class RMBackwardValidationAgent extends BackwardValidationAgent {

    private final static Logger log = LoggerFactory
	    .getLogger(RMBackwardValidationAgent.class);

    @Inject
    RMBackwardValidationAgent(TransactionsLog log, RootSet rs) {
	super(log, rs);
    }

    @Override
    public final boolean validate(TazioTransaction tx) {
	boolean simpleValidationResult = super.validate(tx);
	if (!simpleValidationResult) {
	    return false;
	} else {
	    return areVersionsUpdated(tx);
	}
    }

    /**
     * Checks whether the operation performed by a transaction were performed
     * over the last version of an object. This is useful on RM to prevent that
     * WRM with outdated copies of TransactionalObjectsm, could eventually
     * commit transactions
     * 
     * @return true if all the write operations were performed over the last
     *         version of the object on the root set.
     */
    // TODO: remove this check when a blacklist on outdated clients is
    // implemented by the caching layer
    private final boolean areVersionsUpdated(TazioTransaction tx) {

	for (TransactionalOperation op : tx.getReadOperations()) {
	    OID read = op.getTargetObject();
	    // retrieve the actual object
	    final TransactionalObject obj = getRootSet().getObject(read);
	    if (obj == null) {
		log.error("Object having OID {} was not found in the root set",
			read);
		throw new TransactionalSystemRuntimeException(
			"Expected object " + read
				+ " not found in the root set");
	    }

	    if (op.getOriginalVersion() != obj.getVersion()) {
		return false;
	    }
	}

	return true;
    }
}
